//
// Created by zs on 2022/7/21.
//


#include <vector>
#include <queue>

using namespace std;


/**
 * Definition for a binary tree node.
 **/
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    // 方法一：广度优先搜索
    vector<vector<int>> levelOrder(TreeNode *root) {
        vector<vector<int>> ret;
        if (!root) {
            return ret;
        }
        queue<TreeNode *> q;
        q.push(root);
        while (!q.empty()) {
            int currentLevelSize = q.size();
            ret.push_back(vector<int>());
            for (int i = 1; i <= currentLevelSize; ++i) {
                auto node = q.front();
                q.pop();
                ret.back().push_back(node->val);
                if (node->left)q.push(node->left);
                if (node->right)q.push(node->right);
            }
        }
        return ret;
    }
};

int main() {
    TreeNode *node3 = new TreeNode(3);
    TreeNode *node4 = new TreeNode(4);
    TreeNode *node5 = new TreeNode(3);
    TreeNode *node6 = new TreeNode(4);

    TreeNode *node1 = new TreeNode(2, node3, node4);
    TreeNode *node2 = new TreeNode(2, node6, node5);

    TreeNode *root = new TreeNode(1, node1, node2);

    Solution solution;
    solution.levelOrder(root);

    return 0;
}